const fs=require('fs')
const path=require('path')
const request=require('request')
const __static=path.join('./static')
export default class ListenersEvent{
    download_run(event,url){
        const { shell } = require('electron')
        const name=+new Date()+''+(1 + 100 *  Math.random()).toFixed(0)+path.extname(url)
        const file_path=path.resolve(__static,name)        
        if(!fs.existsSync(__static))
            fs.mkdirSync(path.resolve(__static))
        let stream = fs.createWriteStream(file_path);
        request(url).pipe(stream).on("close",function (err) {
            if(!err){
                shell.openPath(file_path);
                event.reply('download-complete')
            }else
                event.reply('error-msg','文件下载出错')
        });
        
    }

}